package cn.wolfcode.car.business.web.controller.service_item;

import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.base.service.impl.UserServiceImpl;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.service.BpmnInfoService;
import cn.wolfcode.car.business.service.ServiceItemService;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.StringUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.util.Removal;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.ConditionalOnRepositoryType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

/**
 * @Author WaveHi
 * @Date 12/11/2021 下午 3:22
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Controller
@RequestMapping("/business/serviceItem")
public class ServiceItemController {

    @Autowired
    private ServiceItemService serviceItemService;

    @Autowired
    private IUserService userService;

    @Autowired
    private BpmnInfoService bpmnInfoService;

    private static final String prefix = "business/serverItem_view/";

    @RequestMapping("/listPage")
    public String  listPage(Integer pageNo, Integer pageSize, ServiceItemQuery serviceItemQuery) {
        return prefix + "list";
    }

    @RequiresPermissions("business:serviceItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo query(Integer pageNo, Integer pageSize, ServiceItemQuery qo){
        pageNo = pageNo != null ? pageNo : 1;
        pageSize = pageSize != null ? pageSize : 4;
        System.out.println(serviceItemService.getAllServiceItem());
        return serviceItemService.getPartOfServiceItem(qo);
    }

    @RequiresPermissions("business:serviceItem:add")
    @RequestMapping("/addPage")
    public String addPage(Model model){
        return prefix + "add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(ServiceItem serviceItem){
        serviceItemService.add(serviceItem);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:serviceItem:edit")
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id){
        model.addAttribute("serviceItem", serviceItemService.getServiceItem(id));
        return prefix + "edit";
    }

    @RequiresPermissions("business:serviceItem:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ServiceItem serviceItem){
        serviceItemService.update(serviceItem);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:serviceItem:saleOff")
    @RequestMapping("/saleOff")
    @ResponseBody
    public AjaxResult saleOff(Long id){
        serviceItemService.saleOff(id);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:serviceItem:saleOn")
    @RequestMapping("/saleOn")
    @ResponseBody
    public AjaxResult saleOn(Long id){
        serviceItemService.saleOn(id);
        return AjaxResult.success();
    }

    @RequestMapping("/selectAllSaleOnList")
    @ResponseBody
    public TablePageInfo<ServiceItem> selectAllSaleOnList(
            Integer pageNo,
            Integer pageSize,
            ServiceItemQuery qo) {
        //Judge data
        pageNo = pageNo != null ? pageNo : 1;
        pageSize = pageSize != null ? pageSize : 3;
        boolean nameEmpty = StringUtils.isEmpty(qo.getName());
        boolean correct = qo.getServiceCatalog() != null && qo.getServiceCatalog() >= 0 && qo.getServiceCatalog() <= 2;
        //Correct
        if(!nameEmpty) {
            qo.setName(qo.getName().trim());
            qo.setDiscountPrice(new BigDecimal(qo.getName()));
            qo.setName(null);
        }else {
            qo.setName(null);
        }
        if(!correct) {
            qo.setServiceCatalog(null);
        }
        qo.setSaleStatus(null);
        qo.setAuditStatus(null);
        //Query
        return serviceItemService.getPartOfServiceItem(qo);
    }

    @RequestMapping("/auditPage")
    public String auditPage(
            @RequestParam(value = "id", required = false) Long id,
            Model model) {
        if(id != null) {
            model.addAttribute("serviceItem", serviceItemService.getServiceItem(id));

            //店长
            model.addAttribute("directors", userService.queryByRoleKey("shopOwner"));
            //财务
            model.addAttribute("finances", userService.queryByRoleKey("financial"));

            //流程
            BpmnInfo bpmnInfo = bpmnInfoService.queryByType("car_package");
            model.addAttribute("bpmnInfo", bpmnInfo);
            return prefix + "audit";
        }
        return  null;
    }

    @RequestMapping("/startAudit")
    @ResponseBody
    public AjaxResult startAudit(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "bpmnInfoId", required = false) Long bpmnInfoId,
            @RequestParam(value = "director", required = false) Long director,
            @RequestParam(value = "finance", required = false) Long finance,
            @RequestParam(value = "info", required = false) String info) {
        //Judge data
        boolean empty = StringUtils.isEmpty(info);
        //Correct data
        if(!empty) {
            info = info.trim();
        }
        //Start audit
        if(id != null && bpmnInfoId != null && director != null) {
            ServiceItem serviceItem = serviceItemService.getServiceItem(id);
            if(serviceItem == null) {
                return AjaxResult.error("审核对象不存在！");
            }
            if(ServiceItem.AUDITSTATUS_NO_REQUIRED.equals(serviceItem.getAuditStatus())) {
                return AjaxResult.error("无需审核");
            }
            serviceItemService.audit(id, bpmnInfoId, director, finance, info);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

}
